<?php

/**
* @property DB_active_record $db
* @property Service $service
* @property Provider $provider
* @property Organisation_type $organisation_type
* */
class SearchByProviders extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('json');
	}

	function index()
	{
		//$data['serviceList'] = $this->service->getAll() ;
		//parent::add_data( 'serviceList' , $this->service->getAll()  );
		
		//$data['include_view'] = "Search/byService.php" ;
		//$this->load->view("_Shared/_Layout" , $data);
		//parent::view( "Search/View_SearchByService.php"  );
		parent::add_data( 'providerList' , $this->provider->getAll()  );
		parent::view( "SearchByProviders/index.php"  );
	}
	
	function Search ( ) //$searchType , $searchText )
	{
		$searchType = $_POST['searchType'] ;
		$searchText = $_POST['searchText'] ;
		parent::check_null_return_json_error( $searchType , $searchText ) ;
		
		if ( $searchType == "Alphabeth")
		{
			if ( $searchText == "All")
			{
				$providerData = $this->provider->getAll()->result() ;
			}
			else
			{
				$providerData = $this->provider->getProviderList_byAlphabeth( $searchText ) ;
			}
		}
		else if ( $searchType == "Keyword")
		{
			$providerData = $this->provider->getProviderList_byKeyword( $searchText ) ;
		}
		else
		{
			parent::json_return_failed();
		}
		
		echo json_encode( $this->providerListBuilder ( $providerData ) );
	}

	private function providerListBuilder ( $providerData )
	{
		$providerList = array () ;
		if ( $providerData != null )
		{
			foreach ($providerData as $row) {
				$each = array (
					'id' => $row->pro_id
					, 'name' => $row->pro_name
					, 'url' => '/SearchResult/' . $row->pro_id
				);
				array_push ( $providerList , $each );
			}
		}
		
		$ret = array (
			'status' => true
			, 'providerList' => $providerList
		);
		return $ret;
	}
	
		
	function View ( )
	{
		$id = $_POST['hidId'] ;
		parent::check_null_show_error ( $id ) ;
		parent::add_data( 'provider' , $this->provider->getProviderDetail( $id ) );
		parent::view( "SearchByProviders/view.php"  );
	}
}


